//
//  CC_ClassifyCollectionView_CollectionViewCell.m
//  HimalayaMountain
//
//  Created by dllo on 16/7/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CC_ClassifyCollectionView_CollectionViewCell.h"
#import "CCBaseCollectionView.h"
#import "CC_CircularImage_Label_CollectionViewCell.h"  // 自定义样式风格
#import "CC_Classify_Model.h"

@interface CC_ClassifyCollectionView_CollectionViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property(nonatomic, retain) CCBaseCollectionView *collectionView;
@property(nonatomic, retain) NSArray *collectionViewClassifyArray;

@end


@implementation CC_ClassifyCollectionView_CollectionViewCell
- (void)dealloc
{
    [_collectionViewClassifyArray release];
    [_collectionView release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self handleData];
        [self createCollectionView];
        
    }
    return self;
}

-(void)handleData{
    self.collectionViewClassifyArray = [NSArray array];
    [CCNetTool GET:@"http://mobile.ximalaya.com/mobile/discovery/v2/categories?channel=ios-b1&code=43_210000_2102&device=iPhone&picVersion=13&scale=2&version=4.3.98" body:nil headerFile:nil response:CCJSON success:^(id result) {
        NSDictionary *dic = result;
        NSDictionary *disCoberyColumns = [dic objectForKey:@"discoveryColumns"];
//        NSLog(@"%@", disCoberyColumns);
        NSArray *list = [disCoberyColumns objectForKey:@"list"];
        self.collectionViewClassifyArray = [CC_Classify_Model arrayFromArray:list];
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
}

-(void)createCollectionView{
    
    //CGSizeMake(SCREENWIDTH , SCREENHEIGHT / 7);

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREENWIDTH / 5,SCREENHEIGHT / 7 - 2 * SPACED);
    flowLayout.sectionInset = UIEdgeInsetsMake(SPACED, SPACED, SPACED, 0);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = SPACED;
    
  self.collectionView = [[CCBaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT / 7) collectionViewLayout:flowLayout];
     self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.contentView addSubview:_collectionView];
    [flowLayout release];
    [_collectionView release];

    [self.collectionView registerClass:[CC_CircularImage_Label_CollectionViewCell class] forCellWithReuseIdentifier:@"CIRCELL"];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.collectionViewClassifyArray.count;
    
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CC_CircularImage_Label_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CIRCELL" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor redColor];
    cell.classifyModel = [self.collectionViewClassifyArray objectAtIndex:indexPath.row];
    
    return cell;
}


@end
